﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using XEvents.Library;


namespace ExtendedEventsManager
{
    public partial class EditSessionOptionForm : Form
    {
        private Library.ServerMetaData _metadata;
        private SessionEditorForm _parent;
        private bool _userdefined = false;

        public EditSessionOptionForm(Library.ServerMetaData metadata, SessionEditorForm parent, Library.SessionOption sessionoption)
        {
            InitializeComponent();
            _metadata = metadata;
            _parent = parent;
            BindOptionToForm(sessionoption);
            this.Text = "Session Option Editor (" + parent.SessionName + ")";
        }

        public string OptionName
        {
            get { return txt_OptionName.Text; }
        }

        public string OptionValue
        {
            get
            {
                if (_userdefined)
                {
                    return txt_Value.Text;
                }
                else
                {
                    return cmbx_Value.Text;
                }
            }
        }

        private void BindOptionToForm(Library.SessionOption sessionoption)
        {
            foreach (SessionOptionValue sov in _metadata.SessionOptionValues)
            {
                if (sov.Name == sessionoption.Name)
                {
                    if (sov.OptionType == "option_defined")
                    {
                        txt_Value.Visible = false;
                        cmbx_Value.DataSource = sov.OptionValues;
                    }
                    else
                    {
                        cmbx_Value.Visible = false;
                        _userdefined = true;
                        txt_Value.Text = sessionoption.Value;
                    }
                }
            }
            txt_OptionName.Text = sessionoption.Name;
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btn_Ok_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
    }
}
